#/bin/bash

set -e
set -x

lvs_vip=10.0.0.100
lvs_dip=10.0.0.37
nginx01=10.0.0.38
nginx02=10.0.0.39
netname1=ens33

ifconfig $netname1:0 $lvs_vip up
# 1. 添加虚拟服务
ipvsadm -A -t $lvs_vip:80 -s rr
# 2. 添加真实服务器
ipvsadm -a -t $lvs_vip:80 -r $nginx01:80 -g
ipvsadm -a -t $lvs_vip:80 -r $nginx02:80 -g
# 查看
ipvsadm -ln

iplist_path=config
while read -r line
do
    case $str in       
        *'#'*)
            continue
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac  
    
    pass="${parts[0]}" 
    address="${parts[1]}"
    hostname="${parts[2]}"
    if [ $address ];then
        sshpass -p$pass ssh -n $address yum install -y nginx
        sshpass -p$pass ssh -n $address "echo $hostname > /usr/share/nginx/html/index.html"
        sshpass -p$pass ssh -n $address systemctl enable --now nginx
        sshpass -p$pass ssh -n $address 'cat > /etc/sysctl.d/arp.conf <<EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
EOF'
        sshpass -p$pass ssh -n $address sysctl -p /etc/sysctl.d/arp.conf
        sshpass -p$pass ssh -n $address ifconfig lo:0 $lvs_vip/8 up
    fi
done < $iplist_path

ipvsadm --save -n  > /etc/sysconfig/ipvsadm
systemctl enable --now ipvsadm

set +x
set +e